package com.tadpole.music.view.fragment.question;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.luck.picture.lib.permissions.RxPermissions;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.question.FreeExperienceAdapter;
import com.tadpole.music.bean.question.QuestionClassBean;
import com.tadpole.music.iView.question.QuestionClassIView;
import com.tadpole.music.presenter.question.QuestionClassPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.question.FreeAnswerActivity;
import com.tadpole.music.view.activity.question.SimulationExerciseActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;

import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * 免费体验的fragment
 * Created By 孙岩
 * on 2020/9/14
 */
public class FreeExperienceFragment extends BaseFragment implements QuestionClassIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private String mParam1;
    private String mParam2;
    private String mParam3;

    private RecyclerView recyclerView;
    private FreeExperienceAdapter freeExperienceAdapter;
    //获取分类
    private QuestionClassPresenter questionClassPresenter;
    public static FreeExperienceFragment newInstance(String param1, String param2,String param3) {
        FreeExperienceFragment fragment = new FreeExperienceFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        args.putString(ARG_PARAM3, param3);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public int getContentViewId() {
        return R.layout.fragment_free_experience;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            mParam3 = getArguments().getString(ARG_PARAM3);
        }
    }

    @Override
    protected void initView(View mRootView) {
        recyclerView = mRootView.findViewById(R.id.recyclerView);
        //获取分类
        questionClassPresenter = new QuestionClassPresenter();
        questionClassPresenter.attachView(this);
        questionClassPresenter.getQuestion_class(mParam1, mParam2);
    }

    @Override
    public void showQuestionClass(final List<QuestionClassBean.DataBean> list) {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 4);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        freeExperienceAdapter = new FreeExperienceAdapter(getActivity(),list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(freeExperienceAdapter);
        recyclerView.setNestedScrollingEnabled(false);

        freeExperienceAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (Mutils.isNetworkAvailable()) {
                    andPermission(String.valueOf(list.get(position).getId()));
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    //权限
    @SuppressLint("CheckResult")
    private void andPermission(final String question_class_id) {
        RxPermissions rxPermission = new RxPermissions(getActivity());
        rxPermission
                .request(
                        Manifest.permission.MODIFY_AUDIO_SETTINGS,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                ).subscribe(new Consumer<Boolean>() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                //请求权限成功
                if (aBoolean) {
                    Intent intent = new Intent(getActivity(), FreeAnswerActivity.class);
                    intent.putExtra("college_id", "1");
                    intent.putExtra("question_rank_id", "3");
                    intent.putExtra("question_class_id", "303");
                    startActivity(intent);
                } else {//失败
                    String msg = "请点击【去授权】打开相应权限！";
                    showAlert(msg);
                }
            }
        });
    }

    //授权
    public void showAlert(String msg) {
        new AlertDialog.Builder(getActivity()).setMessage(msg).
                setPositiveButton("去授权",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setCancelable(true).show();
    }

    @Override
    public void show401() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }
}
